import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/bezier_bounce_footer.dart';
import 'package:flutter_easyrefresh/bezier_hour_glass_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/model/article_model.dart';
import 'package:village_android/model/menu_model.dart';
import 'package:village_android/pages/browser.dart';
import 'package:village_android/pages/web_view_page.dart';
import 'package:village_android/utils/http_utlis.dart';


class ArticleListPage extends StatefulWidget {
  MenuModel menuModel;

  ArticleListPage({Key key, this.menuModel}) : super(key: key);

  @override
  _ArticleListPageState createState() => _ArticleListPageState();
}
//NewsCell

class _ArticleListPageState extends State<ArticleListPage> {
  List items = [];

  @override
  void initState() {
    super.initState();
    getData();
  }

  void getData() {
    HttpUtils.get("/api/article/list", data: {
      "module": widget.menuModel.module,
      "type": widget.menuModel.type
    }, success: (data) {
      setState(() {
        items = data;
        print(items);
      });
    }, error: (errorMsg) {
      print(errorMsg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.menuModel.title),
        ),
        body: Container(
            child: EasyRefresh(
          header: BezierHourGlassHeader(backgroundColor: Colors.red),
          footer: BezierBounceFooter(backgroundColor: Colors.red),
          child: items.isEmpty
              ? Container(
                  padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
                  child: Center(child: Text('无数据')))
              : getListView(),
          onRefresh: () async {
            getData();
          },
          onLoad: () async {},
        )),
      ),
    );
  }

  Widget getListView() {
    return ListView.separated(
        itemBuilder: (context, index) {
          ArticleModel articleModel = ArticleModel.fromJson(items[index]);
          return _newsCell(context, articleModel);
        },
        separatorBuilder: (context, index) =>
            Divider(height: 1.0, color: Colors.black12),
        itemCount: items.length);
  }

  void onItemClick(ArticleModel item) {
    var page = new WebViewPage(
      title: item.title,
      url: item.url,
    );
    Navigator.push(context, new MaterialPageRoute(builder: (context) => page));

    //BrowserHtmlPage

//    var page = new BrowserHtmlPage(title:item.title,content:item.content,);
//    Navigator.push(context, new MaterialPageRoute(builder: (context) => page));
  }

  //独立Item
  Widget _newsCell(BuildContext context, ArticleModel item) {
    Widget coverWidget(url) {
      return FadeInImage.assetNetwork(
        placeholder: 'assets/empty_image.png',
        image: url,
        width: ScreenUtil().setWidth(240),
        height: ScreenUtil().setHeight(150),
        fit: BoxFit.fitWidth,
      );
    }

    return new GestureDetector(
      onTap: () {
        onItemClick(item);
      },
      child: Container(
        height: ScreenUtil().setHeight(210),
        alignment: Alignment.center,
        padding: EdgeInsets.all(5),
        margin: EdgeInsets.all(5),
        color: Colors.white30,
        child: Row(
          children: <Widget>[
            coverWidget(item.coverUrl),
            Padding(padding: EdgeInsets.only(left: 15)),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Padding(padding: EdgeInsets.only(top: 15)),
                  Text(
                    item.title,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 16,
                      color: Colors.black,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 10)),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: Row(
                      children: <Widget>[
                        Text(
                          item.updateTime + " / ",
                          maxLines: 1,
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.black54,
                          ),
                        ),
                        Text(
                          widget.menuModel.title,
                          maxLines: 1,
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.blueAccent,
                          ),
                        ),
                      ],
                    ),
                  ),


                  Padding(padding: EdgeInsets.only(top: 2)),
                ],
              ),
            ),
//            Icon(Icons.arrow_forward_ios),
          ],
        ),
      ),
    );
  }

  Align initTitle(text) {
    return Align(
      alignment: Alignment.centerLeft,
      child: Row(children: <Widget>[
        SizedBox(
          height: 22.0,
          child: RaisedButton(
            child: Text(text,
                textAlign: TextAlign.left, style: TextStyle(color: Colors.red)),
            color: Colors.white,
            onPressed: () {
            },
            shape: StadiumBorder(side: BorderSide(color: Colors.red)),
          ),
        ),
      ],),
    );
  }
}
